<?php defined('IN_ADMIN') or exit('No permission resources.');?>
<?php include $this->admin_tpl('header', 'admin');?>
<div class="pad-10">
<form name="myform" action="?m=epaper&c=index&a=add&pc_hash=<?php echo $_SESSION['pc_hash']?>" method="post" id="myform"  enctype="multipart/form-data">
<div class="common-form">
	<table width="100%" class="table_form">
		<tr>
			<td width="120">添加方式</td> 
			<td>
			<select name='type' id="type">
				<option value="手动上传">手动上传</option>
				<option value="线上抓取">线上抓取</option>
			</select>
			</td>
		</tr>
		<tr>
			<td width="120">选择文件</td> 
			<td>
			<input type="file" name="file" id="file"  class="input-text" value="" /> <br />支持rar格式
			</td>
		</tr>
		<tr>
			<td width="120">选择日期</td> 
			<td>
			<input type="date" name="date" id="date" class="input-text" value="" /> <br />
			</td>
		</tr>
		<tr>
			<td width="120">进度</td>
			<td><progress id="progress" value="0" max="100"></progress><input type="hidden" name="dosubmit" value="dosubmit" /><h3 id="progress2">0%</h3></td>
		</tr>
</table>

<input name="dosubmit" type="button" id="dosubmit" value="上传" class="dialog" />
</div>
</div>
</form>
<script>
	document.getElementById('dosubmit').onclick = function(){
		// 构造 xhr 请求
        // var type = document.getElementById('type').value;
        // var file = document.getElementById('file').files;
        // var date = document.getElementById('date').value;

        var fm = document.getElementById('myform');

		var p = document.getElementById('progress')
		var p2 = document.getElementById('progress2')

		// console.log(fm)

        var fd = new FormData(fm);
        var xhr = new XMLHttpRequest();
		//接口fd：收到提交的数据后会返回收到的数据
		xhr.open('POST', '?m=epaper&c=index&a=add&pc_hash=<?php echo $_SESSION['pc_hash']?>');
		xhr.responseType = 'text'
		xhr.onload = function(){
			if(this.status != 200){
				console.log(this.responseText)
				alert(this.responseText)
				
				p.value = 0
				p2.innerHTML = "0%"
				return false
			}
			// 关闭弹窗
			window.top.right.location.reload()
			window.top.art.dialog({id:'add'}).close()
		}
		xhr.onerror = function (event) {
			console.log("onerror:",this.event)
			alert("网络错误，请检查是否联网")
		}
		xhr.upload.onprogress = function (even) {
			// console.log(even)
			var loaded = even.loaded; //已上传的大小
			var total = even.total; //总大小
			// console.log("已上传:"+loaded)
			// console.log("总大小:"+total)
			var num = Math.floor((loaded / total) * 100)
			p.value = num - 1
			p2.innerHTML = String(num - 1)+"%"
		}
		xhr.send(fd)
	}
</script>
</body>
</html>